<%- include('css') %>
<link rel="stylesheet" href="/public/js_module/simditor/css/simditor.css">
<div id="app" style="width:980px">
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend>案件转交</legend>
    </fieldset>

    <form class="layui-form" action="" lay-filter="form">

        <div class="layui-form-item">
            <label class="layui-form-label">分配中心</label>
            <div class="layui-input-block">
                <select name="street_id" id="street_id" lay-verify="required" lay-search="" lay-filter="finduser">
                    <option value="" >直接选择或搜索选择</option>
                    <!--<option :value="item.id" v-for="(item,index) in streets" :key="index">{{item.name}}</option>-->
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">分配门店</label>
            <div class="layui-input-block">
                <select name="store_id" id="store" lay-verify="required" lay-search="" lay-filter="user">
                    <option value="" >直接选择或搜索选择</option>
                    <!--<option :value="item.id" v-for="(item,index) in streets" :key="index">{{item.name}}</option>-->
                </select>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" v-bind:disabled="isButtonDisabled" lay-filter="save">确认分配</button>
                <button type="button" v-on:click="doclose" class="layui-btn layui-btn-primary">关闭</button>
            </div>
        </div>
    </form>
</div>

<%- include('script') %>
<!--<script src="/public/js_module/jquery.js"></script>-->
<!--simditor富文本依赖-->
<script src="/public/js_module/simditor/module.js"></script>
<script src="/public/js_module/simditor/hotkeys.js"></script>
<script src="/public/js_module/simditor/uploader.js"></script>
<script src="/public/js_module/simditor/simditor.js"></script>

<script>


    var vm = new Vue({
        el: '#app',
        data:function(){
            return {
                isButtonDisabled:false,
                streets:[],
                load:false,
                id:'<%=id%>'
            }

        },
        mounted:function(){

            var self = this;
            layui.use(['form'], function () {
                var form = layui.form;

                var load = layer.load();

                var v = setInterval(()=>{
                    if(self.load){
                        form.render('select');
                        clearInterval(v);
                        layer.close(load);
                    }
                },1000)

                self.getstreets(form,load,v);

                //监听提交
                form.on('submit(save)', function (data) {
                    self.isButtonDisabled = true;
                    $.ajax({
                        url:'/admin/assign/'+self.id,
                        type:'PUT',
                        data:{data:data.field},
                        success:(res)=>{
                            if(res.code === 200){
                                parent.vm.iframesave()
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                                parent.layer.msg('分配成功!',{icon:1})
                            }else{
                                layer.msg('分配失败:'+res.error+' -- '+res.code,{icon:2})
                            }
                            self.isButtonDisabled = false;
                        }
                    });
                    return false;
                });
                form.on('select(finduser)',function (data) {
                    var load = layer.load();
                    self.getroads(form,data.value,load);
                });
            });

        },

        methods:{

            doclose:function(){
                parent.vm.iframeclose();
                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                parent.layer.close(index); //再执行关闭
            },
            getstreets:function(form,load,v){
                var self = this;
                $.ajax({
                    url:'/admin/center',
                    type:'GET',
                    data:{isPage:false},
                    success:(res)=>{
                        if(res.code == 0){
                            this.streets = res.data;
                            for (var i = 0; i < res.data.length; i++) {
                                $('#street_id').append(`<option value="${ res.data[i].id}">${ res.data[i].name}</option>`);
                                form.render('select');
                            }
                            self.load = true;
                        }else{
                            clearInterval(v);
                            layer.close(load);
                            layer.msg('获取中心数据失败：'+res.error+' -- '+res.code,{icon:2})
                        }
                    },
                    fail:(res)=>{
                        clearInterval(v);
                        layer.close(load);
                        layer.msg('获取中心数据失败!  原因：请求发起失败',{icon:2})
                    }
                });
            },
            getroads:function (form,id,load) {
                $.ajax({
                    url:'/admin/store',
                    type:'GET',
                    data:{isPage:false,center:id},
                    success:(res)=>{
                        if(res.code == 0){
                            for (var i = 0; i < res.data.length; i++) {
                                $('#store').append(`<option value="${ res.data[i].id}">${ res.data[i].name}</option>`);
                                form.render('select');
                            }
                            layer.close(load);
                        }else{
                            layer.close(load);
                            layer.msg('获取门店数据失败：'+res.error+' -- '+res.code,{icon:2})
                        }
                    },
                    fail:(res)=>{
                        layer.close(load);
                        layer.msg('获取门店数据失败!  原因：请求发起失败',{icon:2})
                    }
                });
            }
        }
    })




</script>
